package com.mccree.controller;

import com.alibaba.fastjson.JSON;
import com.mccree.pojo.Department;
import com.mccree.service.DepartmentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@Slf4j
public class DepartmentController {
    final DepartmentService departmentService;

    @Autowired
    public DepartmentController(DepartmentService departmentService) {
        this.departmentService = departmentService;
    }

    /**
     * 拿到全部的部门
     */
    @GetMapping("/getAllDept")
    @ResponseBody
    public String getDeptList(){
        List<Department> departments = departmentService.queryDeptList();
        return JSON.toJSONString(departments);
    }
    /**
     * 根据id获取部门
     */
    @GetMapping("/getDeptById/{id}")
    @ResponseBody
    public String getDeptById(@PathVariable("id")Integer id){
        Department dept = departmentService.getDeptById(id);
        return JSON.toJSONString(dept);
    }

}
